//
//  XLMineChangePwdViewController.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineChangePwdViewController.h"
//#import <BaseModule/UIView+XLVisuals.h>
//#import <BaseModule/UIView+Frame.h>
//#import <BaseModule/CountDown.h>
#import "UIView+XLVisuals.h"
#import "UIView+Frame.h"
#import "CountDown.h"
#define MaxCodeLength 4
@interface XLMineChangePwdViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIImageView *judgeImage;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTF;
/** 倒计时*/
@property (nonatomic, strong) CountDown *countDown;
@property (nonatomic, assign)BOOL isCodeRight;//验证码是否正确
@end

@implementation XLMineChangePwdViewController

- (void)dealloc {
    XLLog(@"%@ delloc",[self class]);
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [_countDown destoryTimer];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"修改密码";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    [self.codeTextField addTarget:self action:@selector(codeTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.phoneLabel.text = [NSString stringWithFormat:@"手机号:%@",UserInfo.phone];
}

- (void)initInterface {
    [self.getCodeButton xl_cornerRadius:10 strokeSize:1 color:kXLButtonColor_BLUE];
    self.getCodeButton.backgroundColor = kXLButtonColor_BLUE;
    
    [self setupRightBarItemWithTitle:@"确认" action:@selector(sureAction:)];
    self.rightBarButtonItemEnable = NO;
}

- (void)initData {
    
}

#pragma mark - Action

- (IBAction)getCodeAction:(UIButton *)sender {
//    XLWeakSelf(self);
    [self requestCode];
    
}

- (void)sureAction:(id)sender {
    if (![self.pwdTF.text isEqualToString:self.confirmPwdTF.text]) {
        [self showErrorWithStatus:@"两次密码不一致请确认您的密码"];
        return;
    }
    
    
    if (self.pwdTF.text.length<6) {
        [self showErrorWithStatus:@"密码最低6位数"];
        return;
    }
    
    self.rightBarButtonItemEnable = NO;
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:UserInfo.userName forKey:@"userName"];
        [parameters setValue:self.pwdTF.text forKey:@"passWord"];
        
        [XLHTTPRequest UM_resetPwdWithParameters:parameters success:^(id responseObject) {
            if ([responseObject[@"status"] isEqualToString:@"1"]) {
                self.rightBarButtonItemEnable = YES;
                [self showSuccessWithStatus:@"修改密码成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            } else {
                [self showErrorWithStatus:[NSString stringWithFormat:@"%@",responseObject[@"info"]]];
                self.rightBarButtonItemEnable = YES;

            }
        } failure:^(NSError *error, NSString *info) {
            self.rightBarButtonItemEnable = YES;

        }];
    
}

/** 获取验证码*/
- (void)requestCode {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:UserInfo.phone forKey:@"mobile"];
    [parameters setValue:@"2" forKey:@"smsTypeId"];
    [XLHTTPRequest UM_getSMSWithParameters:parameters success:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"1"]) {
            XLWeakSelf(self);
            [self.codeTextField becomeFirstResponder];
            [weakself buttonCountDown];
            
        } else {
            //                [FunProgressHUD showErrorWithStatus:responseObject[@"info"]];

        }
    } failure:^(NSError *error, NSString *info) {
            if (error == nil) {
                [self exitLogin:info];
            }
        }];

}
/** 倒计时*/
- (void)buttonCountDown {
    XLWeakSelf(self);
    _countDown = [[CountDown alloc]init];
    [_countDown countDownWithTime:60 completeBlock:^(NSInteger countDown) {
        if (countDown < 1) {
            [weakself.getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [weakself.getCodeButton setBackgroundColor:kXLButtonColor_BLUE];
            
            weakself.getCodeButton.enabled = YES;
        } else {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1];
            [weakself.getCodeButton setTitle:[NSString stringWithFormat:@"%lds后重新获取",countDown] forState:UIControlStateNormal];
            [UIView commitAnimations];
            [weakself.getCodeButton setBackgroundColor:[UIColor grayColor]];
            weakself.getCodeButton.enabled = NO;
        }
    }];
}

/** 判断验证码是否正确*/
- (void)checkCode {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:UserInfo.phone forKey:@"mobile"];
    [parameters setValue:self.codeTextField.text forKey:@"mobileCode"];
    [parameters setValue:@"2" forKey:@"smsTypeId"];
    [XLHTTPRequest UM_checkSMSWithParameters:parameters success:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"1"]) {
            self.judgeImage.hidden = NO;
            self.judgeImage.image = [UIImage imageNamed:@"codeRight"];
            self.isCodeRight = YES;
        } else {
//            [FunProgressHUD showErrorWithStatus:responseObject[@"info"]];
            self.judgeImage.hidden = NO;
            self.judgeImage.image = [UIImage imageNamed:@"codeWrong"];
            self.isCodeRight = NO;
        }
    } failure:^(NSError *error, NSString *info) {
        self.judgeImage.hidden = NO;
        self.judgeImage.image = [UIImage imageNamed:@"codeWrong"];
        self.isCodeRight = NO;
    }];
}
#pragma mark - notification 
- (void)textFieldDidChange:(NSNotification *)notification {
    if (self.pwdTF.text.length > 0 && self.confirmPwdTF.text.length >0 && self.isCodeRight) {
        self.rightBarButtonItemEnable = YES;
    } else {
        self.rightBarButtonItemEnable = NO;
    }
}

- (void)codeTextDidChange:(UITextField *)textField {
    if (textField.text.length == MaxCodeLength) {
        [self checkCode];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
